package com.example.fluentmybatisdemo.helloworld.config;

import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @name: HelloWorldConfig
 * @description: config配置类
 * @author: Zhou
 * @create: 2021-08-22
 **/
@ComponentScan(basePackages = "com.example.fluentmybatisdemo.helloworld")
@MapperScan("com.example.fluentmybatisdemo.helloworld.mapper")
@Configuration
public class HelloWorldConfig {

    /**
      * @Description: 配置数据源
      * @Param: 
      * @return: 
      * @Author: Zhou
      * @Date: 2021/8/22
      */
    @Bean
    public DataSource dataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fluent_mybatis_demo?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }


    /**
      * @description 定义mybatis的SqlSessionFactoryBean
      * @param dataSource
      * @return org.mybatis.spring.SqlSessionFactoryBean
      * @author Zhou
      * @date 2021/8/22
      */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    
    
}
